<template>
  <div class="register-container">
    <div class="register-box">
      <h1 class="system-title">盼寻密友</h1>
      <transition name="fade" mode="out-in">
        <div class="register-form" :key="formKey">
          <h2 class="title">用户注册</h2>
          <transition name="slide-fade" appear>
            <div class="form-group">
              <label for="username">用户名：</label>
              <input
                id="username"
                v-model="registerForm.username"
                class="register-input"
                type="text"
                placeholder="请输入用户名"
                @blur="checkUsername"
                @keyup.enter="handleRegister"
              />
              <transition name="fade" mode="out-in">
                <div v-if="usernameError" class="error-message" :key="usernameError">{{ usernameError }}</div>
              </transition>
              <transition name="fade" mode="out-in">
                <div v-if="usernameValid" class="success-message" :key="usernameValid">用户名可用</div>
              </transition>
            </div>
          </transition>
          <transition name="slide-fade" appear>
            <div class="form-group">
              <label for="email">邮 箱：</label>
              <input
                id="email"
                v-model="registerForm.email"
                class="register-input"
                type="email"
                placeholder="请输入邮箱"
                @blur="validateEmail"
                @keyup.enter="handleRegister"
              />
              <transition name="fade" mode="out-in">
                <div v-if="emailError" class="error-message" :key="emailError">{{ emailError }}</div>
              </transition>
              <transition name="fade" mode="out-in">
                <div v-if="emailValid" class="success-message" :key="emailValid">邮箱格式正确</div>
              </transition>
            </div>
          </transition>
          <transition name="slide-fade" appear>
            <div class="form-group">
              <label for="password">密 码：</label>
              <input
                id="password"
                v-model="registerForm.password"
                class="register-input"
                type="password"
                placeholder="请输入密码"
                @keyup.enter="handleRegister"
              />
              <div class="password-strength">
                <div class="strength-bar">
                  <div :class="['strength-fill', passwordStrengthClass]"></div>
                </div>
                <div class="strength-text">{{ passwordStrengthText }}</div>
              </div>
            </div>
          </transition>
          <transition name="slide-fade" appear>
            <div class="form-group">
              <label for="confirmPassword">确认密码：</label>
              <input
                id="confirmPassword"
                v-model="registerForm.confirmPassword"
                class="register-input"
                type="password"
                placeholder="请再次输入密码"
                @keyup.enter="handleRegister"
              />
              <transition name="fade" mode="out-in">
                <div v-if="passwordMismatch" class="error-message" :key="passwordMismatch">两次输入的密码不一致</div>
              </transition>
            </div>
          </transition>
          <transition name="slide-fade" appear>
            <div class="form-group checkbox-group">
              <input
                id="agreement"
                v-model="registerForm.agreement"
                type="checkbox"
              />
              <label for="agreement">同意《<a href="#" @click.prevent="showTerms">用户协议</a>》和《<a href="#" @click.prevent="showPrivacy">隐私政策</a>》</label>
            </div>
          </transition>
          <transition name="bounce" appear>
            <button class="register-btn" @click="handleRegister" :disabled="isRegistering">
              <span v-if="!isRegistering" class="btn-text">注 册</span>
              <span v-else class="btn-text">注册中...</span>
            </button>
          </transition>
          <transition name="fade" appear>
            <div class="form-links">
              <a href="#" @click.prevent="handleBackToLogin">< 返回登录</a>
            </div>
          </transition>
        </div>
      </transition>
    </div>
    
    <!-- 用户协议弹窗 -->
    <transition name="fade" mode="out-in">
      <div v-if="showTermsModal" class="modal">
        <div class="modal-content">
          <span class="close" @click="showTermsModal = false">&times;</span>
          <h2>用户协议</h2>
          <div class="modal-body">
            <p>这是用户协议的内容...</p>
          </div>
        </div>
      </div>
    </transition>
    
    <!-- 隐私政策弹窗 -->
    <transition name="fade" mode="out-in">
      <div v-if="showPrivacyModal" class="modal">
        <div class="modal-content">
          <span class="close" @click="showPrivacyModal = false">&times;</span>
          <h2>隐私政策</h2>
          <div class="modal-body">
            <p>这是隐私政策的内容...</p>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      registerForm: {
        username: "",
        email: "",
        password: "",
        confirmPassword: "",
        agreement: false
      },
      usernameError: "",
      usernameValid: false,
      emailError: "",
      emailValid: false,
      passwordMismatch: false,
      showTermsModal: false,
      showPrivacyModal: false,
      isRegistering: false
    };
  },
  computed: {
    passwordStrengthClass() {
      const strength = this.calculatePasswordStrength(this.registerForm.password);
      if (strength < 2) return "weak";
      if (strength < 4) return "medium";
      return "strong";
    },
    passwordStrengthText() {
      const strength = this.calculatePasswordStrength(this.registerForm.password);
      if (strength === 0) return "请输入密码";
      if (strength < 2) return "弱";
      if (strength < 4) return "中等";
      return "强";
    }
  },
  methods: {
    // 计算密码强度
    calculatePasswordStrength(password) {
      if (!password) return 0;
      
      let strength = 0;
      if (password.length >= 8) strength++;
      if (/[a-z]/.test(password)) strength++;
      if (/[A-Z]/.test(password)) strength++;
      if (/[0-9]/.test(password)) strength++;
      if (/[^A-Za-z0-9]/.test(password)) strength++;
      
      return strength;
    },
    
    // 检查用户名
    async checkUsername() {
      this.usernameError = "";
      this.usernameValid = false;
      
      if (!this.registerForm.username) {
        this.usernameError = "请输入用户名";
        return;
      }
      
      if (this.registerForm.username.length < 3) {
        this.usernameError = "用户名至少3个字符";
        return;
      }
      
      // 模拟用户名重复检测
      try {
        // 这里应该调用API检查用户名是否已存在
        // const response = await checkUsername(this.registerForm.username);
        // 模拟API响应
        const isUsernameTaken = this.registerForm.username === "admin" || this.registerForm.username === "test";
        
        if (isUsernameTaken) {
          this.usernameError = "用户名已存在";
        } else {
          this.usernameValid = true;
        }
      } catch (error) {
        console.error("检查用户名出错:", error);
        this.usernameError = "检查用户名时出错，请稍后再试";
      }
    },
    
    // 验证邮箱格式
    validateEmail() {
      this.emailError = "";
      this.emailValid = false;
      
      if (!this.registerForm.email) {
        this.emailError = "请输入邮箱";
        return;
      }
      
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      if (!emailRegex.test(this.registerForm.email)) {
        this.emailError = "邮箱格式不正确";
        return;
      }
      
      this.emailValid = true;
    },
    
    // 验证表单
    validateForm() {
      // 验证用户名
      if (!this.registerForm.username) {
        this.usernameError = "请输入用户名";
        return false;
      }
      
      if (!this.usernameValid) {
        this.usernameError = "请输入有效的用户名";
        return false;
      }
      
      // 验证邮箱
      if (!this.registerForm.email) {
        this.emailError = "请输入邮箱";
        return false;
      }
      
      if (!this.emailValid) {
        this.emailError = "请输入有效的邮箱";
        return false;
      }
      
      // 验证密码
      if (!this.registerForm.password) {
        this.$message.error("请输入密码");
        return false;
      }
      
      if (this.registerForm.password.length < 6) {
        this.$message.error("密码长度至少6位");
        return false;
      }
      
      // 验证确认密码
      if (this.registerForm.password !== this.registerForm.confirmPassword) {
        this.passwordMismatch = true;
        this.$message.error("两次输入的密码不一致");
        return false;
      }
      
      // 验证协议同意
      if (!this.registerForm.agreement) {
        this.$message.error("请同意《用户协议》和《隐私政策》");
        return false;
      }
      
      return true;
    },
    
    // 处理注册
    async handleRegister() {
      if (!this.validateForm()) {
        return;
      }
      
      // 设置注册状态
      this.isRegistering = true;
      
      try {
        // 这里应该调用注册API
        // const response = await register(this.registerForm);
        // 模拟注册成功
        this.$message.success("注册成功，请登录");
        this.$router.push("/login");
      } catch (error) {
        console.error("注册出错:", error);
        this.$message.error("注册失败，请稍后再试");
      } finally {
        // 重置注册状态
        this.isRegistering = false;
      }
    },
    
    // 显示用户协议
    showTerms() {
      this.showTermsModal = true;
    },
    
    // 显示隐私政策
    showPrivacy() {
      this.showPrivacyModal = true;
    },
    
    // 返回登录页面
    handleBackToLogin() {
      this.$router.push("/login");
    }
  }
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.register-box {
  width: 100%;
  max-width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
  overflow: hidden;
  animation: slideUp 0.6s ease-out;
}

.system-title {
  text-align: center;
  margin-bottom: 30px;
  color: #1890ff;
  font-size: 28px;
  font-weight: 600;
}

.register-form {
  padding: 30px 40px;
}

.title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
}

.register-input {
  width: 100%;
  padding: 15px 15px 15px 45px;
  border: 2px solid #e1e5e9;
  border-radius: 10px;
  font-size: 16px;
  transition: all 0.3s ease;
  box-sizing: border-box;
  background: #f8f9fa;
}

.register-input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
  background: white;
}

.checkbox-group {
  display: flex;
  align-items: center;
}

.checkbox-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.checkbox-group input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: #667eea;
}

.checkbox-group label {
  margin-bottom: 0;
  font-size: 14px;
  color: #666;
}

.checkbox-group a {
  color: #667eea;
  text-decoration: none;
  transition: color 0.3s ease;
}

.checkbox-group a:hover {
  color: #764ba2;
  text-decoration: underline;
}

.register-btn {
  width: 100%;
  padding: 15px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 10px;
  margin-bottom: 20px;
}

.register-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

.register-btn:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.form-links {
  text-align: center;
  margin-top: 20px;
}

.form-links a {
  color: #667eea;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s ease;
}

.form-links a:hover {
  color: #764ba2;
  text-decoration: underline;
}

.error-message {
  color: #ff4d4f;
  font-size: 12px;
  margin-top: 5px;
}

.success-message {
  color: #52c41a;
  font-size: 12px;
  margin-top: 5px;
}

.password-strength {
  margin-top: 10px;
}

.strength-bar {
  width: 100%;
  height: 6px;
  background-color: var(--gray-3);
  border-radius: 3px;
  overflow: hidden;
}

.strength-fill {
  height: 100%;
  width: 0;
  transition: width 0.3s ease;
}

.strength-fill.weak {
  width: 33%;
  background-color: #ff4d4f;
}

.strength-fill.medium {
  width: 66%;
  background-color: #faad14;
}

.strength-fill.strong {
  width: 100%;
  background-color: #52c41a;
}

.strength-text {
  font-size: 12px;
  color: #666;
  margin-top: 5px;
}

/* 弹窗样式 */
.modal {
  display: block;
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
}

.modal-content {
  background-color: var(--white);
  margin: 10% auto;
  padding: var(--spacing-md);
  border: 1px solid var(--border-color);
  width: 80%;
  max-width: 600px;
  border-radius: 8px;
  position: relative;
}

.close {
  color: #aaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
  position: absolute;
  right: 20px;
  top: 10px;
  cursor: pointer;
}

.close:hover,
.close:focus {
  color: black;
  text-decoration: none;
}

.modal-body {
  margin-top: var(--spacing-md);
  max-height: 60vh;
  overflow-y: auto;
}

.modal-body p {
  line-height: 1.6;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 移动端优化 */
@media (max-width: 767px) {
  .register-box {
    margin: 10px;
    border-radius: 15px;
  }
  
  .system-title {
    font-size: 24px;
    margin-bottom: 20px;
  }
  
  .title {
    font-size: 20px;
    margin-bottom: 20px;
  }
  
  .register-input {
    font-size: 16px; /* 防止iOS缩放 */
  }
  
  .checkbox-group label {
    font-size: 13px;
  }
  
  .form-links a {
    font-size: 13px;
  }
  
  .modal-content {
    width: 95%;
    padding: 20px;
  }
}
</style>